package com.woniuxy.sales.controller;

import com.woniuxy.sales.common.enumeration.BusinessCode;
import com.woniuxy.sales.model.vo.CartItemVo;
import com.woniuxy.sales.model.vo.CartVo;
import com.woniuxy.sales.service.CartService;
import com.woniuxy.sales.untils.Assert;
import com.woniuxy.sales.untils.JwtTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车接口")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private JwtTemplate jwtTemplate; //要实现会话跟踪，从Token中获取用户id

    @GetMapping("/getCart")
    @ApiOperation("查询购物车")
    public ResponseEntity getCart(@RequestHeader(name = "Authorization", required = false) String token) {
        //校验Token
        Assert.error(! jwtTemplate.validateToken(token), BusinessCode.INVALID_TOKEN);
        //查询用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        log.info("用户ID:{}", userId);
        //调用业务层查询购物车
        CartVo cartvo = cartService.getCart(Integer.valueOf(userId));
        log.info("购物车信息:{}", cartvo);
        return ResponseEntity.ok(cartvo);
    }
    @PostMapping("/add/{goodsId}")
    @ApiOperation("添加商品到购物车")
    public ResponseEntity addCartItem(@RequestHeader(name = "Authorization", required = false) String token,
            @PathVariable("goodsId") Integer goodsId) {
        //获取用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        //调用业务层添加商品到购物车
        CartVo cartvo = cartService.addCartItem(Integer.valueOf(userId), goodsId);
        return ResponseEntity.ok(cartvo);
    }

    @DeleteMapping("/remove/{goodsId}")
    @ApiOperation("删除购物车一项")
    public ResponseEntity remove(@RequestHeader(name = "Authorization", required = false) String token,
                                 @PathVariable Integer goodsId) {
        //获取用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        //调用业务层创建订单
        cartService.remove(Integer.valueOf(userId),goodsId);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/removeAll")
    @ApiOperation("清空购物车")
    public ResponseEntity removeAll(@RequestHeader(name = "Authorization", required = false) String token) {
        //获取用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        //调用业务层创建订单
        cartService.removeAll(Integer.valueOf(userId));
        return ResponseEntity.ok().build();
    }
    @PostMapping("/removeBySelection")
    @ApiOperation("删除选中的购物车")
    public ResponseEntity removeBySelection(@RequestHeader(name = "Authorization", required = false) String token,
                                            @RequestBody List<CartItemVo> cartItemVos) {
        //获取用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        //调用业务层创建订单
        cartService.removeBySelection(Integer.valueOf(userId),cartItemVos);
        return ResponseEntity.ok().build();
    }





    @PutMapping("/updateQuantity/{goodsId}/{quantity}")
    @ApiOperation("修改商品数量")
    public ResponseEntity updateQuantity(@RequestHeader(name = "Authorization", required = false) String token,
                                 @PathVariable Integer goodsId,@PathVariable Integer quantity) {
        //获取用户ID
        String userId = jwtTemplate.getValue(token, "userId");
        //调用业务层创建订单
        cartService.updateQuantity(Integer.valueOf(userId),goodsId,quantity);
        return ResponseEntity.ok().build();
    }






}